package cn.hbu.zlfw.patent.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.hbu.zlfw.admin.entity.ScienceExpert;
import cn.hbu.zlfw.admin.service.ScienceExpertService;
import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.annotation.OperatorType;
import cn.hbu.zlfw.common.util.DicSubjectUtil;
import cn.hbu.zlfw.common.util.SessionUtil;
import cn.hbu.zlfw.dic.service.DicPatentStateService;
import cn.hbu.zlfw.dic.service.DicSchoolService;
import cn.hbu.zlfw.dic.service.DicSubjectService;
import cn.hbu.zlfw.patent.entity.Patent;
import cn.hbu.zlfw.patent.service.PatentService;
import cn.hbu.zlfw.research.entity.Researcher;

/**
 * 
 * @ClassName: PatentController 
 * @Description: 专利控制层
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年11月2日 下午7:03:36  
 * @version V1.0
 */
@Controller("patentController")
@RequestMapping("/patent")
public class PatentController {
	/**
	 * 日志
	 */
	public final Logger logger=LoggerFactory.getLogger(PatentController.class);
	/**
	 * 注入HttpServletRequest
	 */
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private DicSchoolService dicSchoolService;
	
	@Autowired
	private ScienceExpertService scienceExpertService;
	/**
	 * 注入service
	 */
	@Autowired
	private PatentService patentService;
	@Autowired
	private DicPatentStateService dicPatentStateService;
	@Autowired
	private DicSubjectService dicSubjectService;
	/**
	 * 
	 * @Title: toPatentlist 
	 * @Description: 转向前台首页
	 * @return String   
	 * @throws
	 */
	@OperatorLog(type=OperatorType.OTHER,description="科研人员登录成功，转向前台首页")
	@RequestMapping("/topatentlist.action")
	public String toPatentlist(@RequestParam(value="patentState",required=false)Integer patentState,
			@RequestParam(value="name",required=false)String name,
			ModelMap map){
		List<Patent> patents=this.patentService.listPatentByParamsToResearcher(patentState, name,
				((Researcher) SessionUtil.getLoginUser(request)).getId());
		if(patents == null || patents.isEmpty()) {
			map.put("errorNullMsg", "没有专利...");
		}else {
			map.put("patents", patents);
			//处理学科字段
			map.put("patentSubjectsChars",DicSubjectUtil.parseSubjectByPatents(patents,dicSubjectService));
		}
		map.put("dicPatentStates", dicPatentStateService.findAll());
		map.put("patentStatechecked", 0);
		if(name != null && name!="")
			map.put("name", name);
		return "zlxt/patentlist";
	}
	
	@OperatorLog(type=OperatorType.OTHER,description="转向接单科技服务人员信息页面")
	@RequestMapping("/scienceExpertInformation.html")
	public String updateScienceExpertUI(ModelMap map,@RequestParam(value="scienceId",required=false)String scienceId) {
		HttpSession session = request.getSession();
		if(scienceId != null && scienceId != "") {
			session.setAttribute("sessionUnitScienceId", scienceId);
		}
		map.put("dicSubjects", this.dicSubjectService.findAll());
		map.put("dicSchools", this.dicSchoolService.findAll());
//		UnitAdmin unitAdmin = (UnitAdmin) SessionUtil.getLoginUser(request);
//		map.put("unitSchoolType", unitAdmin.getSchoolType());
		ScienceExpert scienceExpert = this.scienceExpertService
				.getById(session.getAttribute("sessionUnitScienceId").toString());
		if(scienceId != null && scienceId != "") {
			map.put("scienceExpert", scienceExpert);
		}
		return "zlxt/scienceInformation";
	}
}
